package reflect;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import entity.User;

public class TestGetter {

	/**
	 * @param args
	 * @throws InvocationTargetException 
	 * @throws IllegalArgumentException 
	 * @throws IllegalAccessException 
	 */
	public static void main(String[] args) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
		User user=new User();
		user.setId(10);
		user.setN("z3");
		user.setPassword("12345");
		invokeGetter(user,"name");
		invokeGetter(user,"id");
		invokeGetter(user,"password");
	}
	public static void invokeGetter(Object obj,String fieldName) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException{
		Method[] methods = obj.getClass().getMethods();
		String methodName="get"+fieldName.substring(0, 1).toUpperCase()+fieldName.substring(1);//foo-->getFoo
		
		Object returnValue=null;
		for(Method method:methods){
			if(method.getName().equals(methodName)){
					returnValue=method.invoke(obj,new Object[]{});
					break;
			}
		}
		System.out.println(returnValue);
	}
}
